package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"sanrenx.com/xyxf/app/services"
	"sanrenx.com/xyxf/pkg/app"
	"sanrenx.com/xyxf/pkg/e"
	"sanrenx.com/xyxf/pkg/utils"
)

// @Tags 音乐
// @Summary 音乐歌单列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param mode query integer false "类型"
// @Param category query integer false "分类ID"
// @Param order query string false "排序 hot or new"
// @Success 200 {object} app.Response
// @Router  /app/v2/music/playlist [get]
func MusicPlaylistGetPageV2(c *gin.Context) {

	var (
		dataSrv services.MusicSrv
		params  struct {
			PageNum  int     `form:"page" binding:"required,number"`
			PageSize int     `form:"size" binding:"number"`
			Mode     *string `form:"mode"`
			Category int     `form:"category"`
			OrderBy  string  `form:"order"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPageSize(pageNum, params.PageSize)
	if res, err := dataSrv.GetPagePlaylistByMode2Category(int64(offset), int64(size), params.Mode, []int{params.Category}, false, params.OrderBy); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetPlaylistFail, nil)
}

// @Tags 音乐
// @Summary 歌单收录音频列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param id query integer true "歌单ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/music/playlist/audio [get]
func MusicGetPlaylist(c *gin.Context) {
	var (
		dataSrv services.MusicSrv
		params  struct {
			PageNum    int    `form:"page" binding:"required,number"`
			PlaylistID uint64 `form:"id" binding:"required,number"`
		}
	)
	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPage(pageNum)
	if res, err := dataSrv.GetPageAudioByPlaylist(params.PlaylistID, int64(offset), int64(size)); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //文章列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetPlaylistAudioFail, nil)
}

// @Tags 音乐
// @Summary 音乐人物音频列表
// @Accept application/json
// @Produce  json
// @Param page query integer true "页码"
// @Param keyman query integer false "人物"
// @Param order query string false "排序 hot or new"
// @Success 200 {object} app.Response
// @Router  /app/v2/music/keyman/audio [get]
func MusicKeymanAudioGetPageV2(c *gin.Context) {

	var (
		dataSrv services.MusicSrv
		params  struct {
			PageNum  int    `form:"page" binding:"required,number"`
			PageSize int    `form:"size" binding:"number"`
			Keyman   int    `form:"keyman"`
			OrderBy  string `form:"order"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	pageNum := params.PageNum
	offset, size := utils.GetPageSize(pageNum, params.PageSize)
	if res, err := dataSrv.GetPageAudioByKeyman(int64(offset), int64(size), []int{params.Keyman}, false, params.OrderBy); err == nil {
		//返回信息
		appG.Response(http.StatusOK, e.SUCCESS, map[string]interface{}{
			"page": pageNum,  //页码
			"size": len(res), //数量
			"list": res,      //列表
		})
		return
	}

	appG.Response(http.StatusInternalServerError, e.ErrorGetAudioFail, nil)
}

// @Tags 音乐
// @Summary 歌单详情
// @Accept application/json
// @Produce json
// @Param id query integer true "歌单ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/music/playlist/details [get]
func MusicGetPlaylistDetailsV2(c *gin.Context) {

	var (
		musicSrv services.MusicSrv
		params   struct {
			PlaylistID uint64 `form:"id" binding:"required,number"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//获取歌单详情
	res, err := musicSrv.GetPlaylistById(params.PlaylistID)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorGetPlaylistFail, nil)
		return
	}

	//返回信息
	appG.Response(http.StatusOK, e.SUCCESS, res)
}

// @Tags 音乐
// @Summary 音频详情
// @Accept application/json
// @Produce json
// @Param id query integer true "音频ID"
// @Success 200 {object} app.Response
// @Router  /app/v2/music/audio/details [get]
func MusicGetAudioDetailsV2(c *gin.Context) {

	var (
		musicSrv services.MusicSrv
		params   struct {
			AudioID uint64 `form:"id" binding:"required,number"`
		}
	)

	appG := app.Gin{C: c}
	//校验绑定参数
	if err := c.ShouldBind(&params); err != nil {
		app.MarkErrors(err)
		appG.Response(http.StatusBadRequest, e.InvalidParams, nil)
		return
	}

	//获取音频详情
	res, err := musicSrv.GetAudioById(params.AudioID)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ErrorGetAudioFail, nil)
		return
	}

	//返回信息
	appG.Response(http.StatusOK, e.SUCCESS, res)
}
